// pages/compony-pages/job-detail/job-detail.js
const app = getApp();
let store = app.store;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        jobDetail:{},
        flag:0
    },
    /**
     * ===================事件处理===================
     */
    editJob(){
        app.globalData.enterpriseInfo = this.data.jobDetail.EnterpriseInfo;
        app.globalData.editJobInfo = this.data.jobDetail.JobInfo;
        wx.navigateTo({
            url: '/pages/compony-pages/issue-job/issue-job?flag=1'
        })
    },
    clickCompony(){
        let obj = {
            enterpriseId:'',
            userId:''
        };
        obj.enterpriseId = this.data.jobDetail.EnterpriseInfo.Id;
        obj.userId = this.data.jobDetail.JobInfo.Id;
        app.globalData.enterpriseInfo = this.data.jobDetail.EnterpriseInfo;
        wx.navigateTo({
            url: '/pages/compony-pages/compony-info/compony-info?parmas='+JSON.stringify(obj)
        })
    },
    clickConsult(){
        let account = this.data.jobDetail.UserInfo.Id;
        let nickName = this.data.jobDetail.UserInfo.NickName;
        this.getResumeInfoById(function(){
            store.dispatch({
                type: 'CurrentChatTo_Change',
                payload: `p2p-${account}`
            })
            wx.navigateTo({
                url: `/partials/chating/chating?chatTo=${account}&type=p2p&title=${nickName}`,
            });
        });
    },
     /**
     * ===================事件处理===================
     */
    /**
     * -------------------网络请求部分-------------------
     */
    getResumeInfoById(func) {
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getResumeInfoById,
            data: {
                userInfoId: app.userInfo.Id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function (res) {
                console.log('getResumeInfoById response', res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        let isOpen = res.data.BodyContent.IsOpen;
                        if(isOpen == 1){
                            func();
                        }
                        else{
                            wx.showModal({
                                title: '提示',
                                content: '请完善并公开你的简历后，再与企业聊天',
                                showCancel: true,
                                cancelText: '取消',
                                cancelColor: '#666666',
                                confirmText: '确定',
                                confirmColor: '#4EB262',
                                success: function(res) {
                                    if(res.confirm){
                                        wx.navigateTo({
                                            url: '/pages/new-pages/main-page/main-page'
                                        });
                                    }
                                }
                            })
                        }
                    }
                } else {
                    Toast(res.data.Message);
                }
            },
            fail: function (res) {
                console.log("getResumeInfoById fail", res)
            },
            complete: function (res) {
                wx.hideLoading();
            },
        })
    },
    getJobDetail(id){
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.getJobDetail,
            data: {
                jobInfoId:id,
                currentUserInfoId:app.userInfo.Id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function (res) {
                console.log(res.data.BodyContent);
                let hourlySalary = parseInt((res.data.BodyContent.JobInfo.SalaryMin * 1000) / 80);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    that.setData({
                        jobDetail: res.data.BodyContent,
                        hourlySalary: hourlySalary
                    })
                }
            },
            fail: function (res) { console.log("getEnterpriseJobList fail", res) },
            complete: function (res) { wx.hideLoading() },
        })
    },
    /**
     * -------------------网络请求部分-------------------
     */


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let parmas = JSON.parse(decodeURIComponent(options.parmas));
        let jobId = parmas.jobId;
        let jobName = parmas.jobName;
        if(jobId){
            this.getJobDetail(jobId);
        }
        if(jobName){
            wx.setNavigationBarTitle({
                title: jobName
            })
        }
        if(parmas.flag){
            let flag = parmas.flag;
            if (app.userInfo.IsEnterpriseVerified == 2 && flag != 1){
                flag = 0;
            }
            this.setData({
                flag: flag
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})